package com.orange.adminapi.controller.coupon;

import com.orange.core.pojo.R;
import com.orange.core.pojo.entity.coupon.CouponCategory;
import com.orange.core.service.coupon.CouponCategoryService;
import com.orange.core.validator.group.Update;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 优惠券分类
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/couponCategory")
public class CouponCategoryController {

    private final CouponCategoryService couponCategoryService;

    /**
     * 查询
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).COUPON_CATEGORY_LIST.permission.identifier)")
    @PostMapping("/list")
    public R<List<CouponCategory>> list() {
        return R.ok(couponCategoryService.list());
    }

    /**
     * 新增
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).COUPON_CATEGORY_SAVE.permission.identifier)")
    @PostMapping
    public R<Boolean> save(@RequestBody @Validated CouponCategory couponCategory) {
        return R.ok(couponCategoryService.save(couponCategory.withId(null)));
    }

    /**
     * 编辑
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).COUPON_CATEGORY_UPDATE.permission.identifier)")
    @PutMapping
    public R<Boolean> update(@RequestBody @Validated(Update.class) CouponCategory couponCategory) {
        return R.ok(couponCategoryService.updateById(couponCategory));
    }

    /**
     * 删除
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).COUPON_CATEGORY_DELETE.permission.identifier)")
    @DeleteMapping("/{id}")
    public R<Boolean> del(@PathVariable int id) {
        return R.ok(couponCategoryService.del(id));
    }
}
